// strncpy(s1, s2, n)函数把s2中的n个字符拷贝到s1中，截断s2，或者有必要的话在末尾添加空字符。
// 如果s2的长度是n或者多于n，目标字符串不能以空字符结尾。
// 该函数返回s1.
// 使用循环输入测试

#include <stdio.h>
#define SIZE 30
char * mystrncpy(char *dest, char *src, int n);

/*
int main(void)
{
	char dest[SIZE];
	char * source;
	int i;

	printf("Enter a String as a source (blnk to quit.): ");
	fgets(source, SIZE, stdin);
	printf("Enter number of char you need to copy: ");
	scanf("%d", &i);
	
	while(*source != '\n') {
		mystrncpy(dest, source, i);
		puts("Done!\n Now the dest string is: ");
		puts(dest);
		while(getchar() == '\n')
			break;
	}


	return 0;
}
*/

char * mystrncpy(char *dest, char *src, int n)
{
	int count = 0;
	
	while(*src != '\0' && count < n)
		*(dest + count++) = *src++;

	
	if (count < n)
		while(count < n)
			*(dest + count++) = '\0';

	return dest;
}
